import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '@v/Home/index.vue'
import {isLogin} from '@u/index'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      meta: {
        needAuth: false
      },
      component: HomePage
    },
    {
      path: '/cate',
      name: 'cate',
      meta: {
        needAuth: false
      },
      component: () => import('@v/Cate/index.vue')
    },
    {
      path: '/cart',
      name: 'cart',
      meta: {
        needAuth: true
      },
      component: () => import('@v/Cart/index.vue')
    },
    {
      path: '/user',
      name: 'user',
      meta: {
        needAuth: true
      },
      component: () => import('@v/User/index.vue')
    },
    {
      path: '/itemLists',
      name: 'itemLists',
      meta: {
        needAuth: false
      },
      component: () => import('@v/ItemLists/index.vue')
    },
    {
      path: '/detail',
      name: 'detail',
      meta: {
        needAuth: false
      },
      component: () => import('@v/Detail/index.vue')
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        needAuth: false
      },
      component: () => import('@v/Login/index.vue')
    },
    {
      path: '/reg',
      name: 'reg',
      meta: {
        needAuth: false
      },
      component: () => import('@v/Register/index.vue')
    }
  ]
})

router.beforeEach((to, from) => {
  // 判断是不是需要登录
  if (to.meta.needAuth) {
    // 需要登录
    if (isLogin()) {
      return true
    } else {
      return '/login'
    }
  }
})
export default router
